﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;
using Chaliy.DbPro.Generators.TextAuthoring;

namespace Chaliy.DbPro.Generators
{
	public abstract class TextGenerator : Generator
	{
		#region Fields

		private int minimumLength;
		private int maximumLength;
		private Random randomGenerator;
		private ITextAuthor author;		
		private string result;

		#endregion

		#region Properties

		[Input(Name = "Minimum Length", Description = "Specify minimum length of the genereted string. 0 is totaly aceptable.")]
		public int MinimumLength
		{
			get
			{
				return this.minimumLength;
			}
			set
			{
				this.minimumLength = value;
			}
		}

		[Input(Name = "Maximum Length", Description = "Specify maxumum length of the genereted string")]
		public int MaximumLength
		{
			get
			{
				return this.maximumLength;
			}
			set
			{
				this.maximumLength = value;
			}
		}

		[Output]
		public string Result
		{
			get
			{
				return this.result;
			}		
		}

		protected Random RandomGenerator
		{
			get
			{
				if (this.randomGenerator == null)
				{
					this.randomGenerator = new Random();
				}
				return this.randomGenerator;
			}
		}

		protected ITextAuthor Author
		{
			get
			{
				if (this.author == null)
				{
					this.author = this.CreateAuthor();
				}
				return this.author;
			}			
		}				

		#endregion

		#region Methods

		protected abstract ITextAuthor CreateAuthor();

		protected override void OnInitialize(GeneratorInit initInfo)
		{
			this.randomGenerator = null;
			this.author = null;
			base.OnInitialize(initInfo);
		}

		protected override void OnGenerateNextValues()
		{
			base.OnGenerateNextValues();

			int length = this.RandomGenerator.Next(this.MinimumLength, this.MaximumLength);

			this.result = this.Author.Generate(length);
		}

		#endregion		
	}
}
